home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / WORLDREP.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  6KB  |  266 lines

  1. #include <InterViews\interact.h>
  2. #include <InterViews\canvas.h>
  3. #include <InterViews\X11\palette.h>
  4. #include <InterViews\X11\worldrep.h>
  5. #include <InterViews\itable.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. const int MAXSIZE = 100;
  10. const int MAXARGUMENT = 25;
  11.  
  12. HANDLE hInstance_;
  13. HANDLE hPrevInstance_;
  14. LPSTR  lpCmdLine_;
  15. int    nCmdShow_;
  16.  
  17. int    argc_ = 0;
  18. char*  argv_[MAXARGUMENT];
  19.  
  20. int    argc;
  21. char** argv;
  22.  
  23. /*
  24.  *  IVMain ist die Main-Funktion der InterViews Anwendung. Sie wird von
  25.  *  WinMain, das selbst in diesem Header vor dem Anwender versteckt ist,
  26.  *  aufgerufen.
  27.  */
  28.  
  29. extern int IVMain(int, char**);
  30.  
  31. /*
  32.  *  Die Message Queue der Applikation wird vergrößert.
  33.  */
  34.  
  35. void SetMessageQueueLength () {
  36.     int size = MAXSIZE;
  37.     while (SetMessageQueue(size) == 0) {
  38.         size -= 5;
  39.     }
  40. }
  41.  
  42. /*
  43.  *  Die beiden Parameter argc und argv werden, wie unter C bekannt und
  44.  *  auch unter Interviews erwartet, erzeugt. IV verändert argc und argv
  45.  *  während des Programmablaufes. Daher ist es notwendig, die Kommando-
  46.  *  zeilenparameter in einem eigenen Puffer abzustellen (argc_, argv_).
  47.  */
  48.  
  49. void SetCommandLineArguments () {
  50.     char* arg;
  51.     char module[128];
  52.  
  53.                      /* argv_[0] - Programmname */
  54.  
  55.     GetModuleFileName(hInstance_, module, 128);
  56.     int len = strlen(module)+1;
  57.     argv_[argc_] = new char(len);
  58.     strcpy(argv_[argc_], module);
  59.     argc_++;
  60.  
  61.                      /* argv_[i] - Kommandozeilenparameter */
  62.  
  63.     arg = strtok(lpCmdLine_, " ");
  64.     if (arg) {
  65.          argv_[argc_++] = arg;
  66.          while ((arg = strtok(NULL, " ")) != NULL) {
  67.              argv_[argc_++] = arg;
  68.          }
  69.     }
  70.  
  71.                      /* argc, argv */
  72.  
  73.     argc = argc_;
  74.     argv = new char*[argc];
  75.     for (int i = 0; i < argc; i++) {
  76.         argv[i] = argv_[i];
  77.     }
  78. }
  79.  
  80. void DeleteCommandLineArguments () {
  81.     delete  argv_[0];
  82. }
  83.  
  84. /*
  85.  *  Diese Funktion ist der Eintrittspunkt von Windows. Sie soll vor dem
  86.  *  InterViews Anwender versteckt bleiben. Der Anwender hat damit das von
  87.  *  X gewohnte Bild seines Hauptprogrammes (einzige Konvention:
  88.  *  IVMain(int, char**) statt main(int, char**).
  89.  */
  90.  
  91. int PASCAL WinMain(
  92.     HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow
  93. ) {
  94.     hInstance_     = hInstance;
  95.     hPrevInstance_ = hPrevInstance;
  96.     lpCmdLine_     = lpCmdLine;
  97.     nCmdShow_      = nCmdShow;
  98.  
  99.     SetMessageQueueLength();
  100.     SetCommandLineArguments();
  101.  
  102.     IVMain(argc, argv);
  103.  
  104.     DeleteCommandLineArguments();
  105.     return(NULL);
  106. }
  107.  
  108. extern "C" {
  109. long FAR PASCAL IVWindowProc (HWND     hWnd,
  110.                   unsigned message,
  111.                   WORD     wParam,
  112.                   LONG     lParam
  113. ) {
  114.     Interactor* i;
  115.     MSG oldMsg;
  116.  
  117.     switch (message) {
  118.  
  119.     case WM_SIZE:
  120.         while (PeekMessage(
  121.                &oldMsg, hWnd, WM_SIZE, WM_SIZE, PM_REMOVE | PM_NOYIELD
  122.            ));
  123.  
  124.     case WM_MOVE:
  125.         while (PeekMessage(
  126.                &oldMsg, hWnd, WM_MOVE, WM_MOVE, PM_REMOVE | PM_NOYIELD
  127.            ));
  128.  
  129.     case WM_SHOWWINDOW:
  130.     case WM_PAINT:
  131.     case WM_SETFOCUS:
  132.     case WM_KILLFOCUS:
  133.         PostMessage(hWnd, message, wParam, lParam);
  134.         break;
  135.  
  136.     case WM_ERASEBKGND:
  137.         if (_world->itable()->Find(i, (void*)hWnd)) {
  138.         RECT rect;
  139.  
  140.         if (GetUpdateRect(hWnd, &rect, false)) {
  141.             HBRUSH hBrush, holdBrush;
  142.  
  143.             hBrush = (HBRUSH)i->GetCanvas()->GetBackground();
  144.             UnrealizeObject(hBrush);
  145.             holdBrush = SelectObject(wParam, hBrush);
  146.             PatBlt(
  147.             wParam,
  148.             rect.left,
  149.             rect.top,
  150.             rect.right - rect.left,
  151.             rect.bottom - rect.top,
  152.             PATCOPY
  153.             );
  154.             SelectObject(wParam, holdBrush);
  155.             return (1L);
  156.         }
  157.         }
  158.         break;
  159.  
  160.     case WM_PALETTECHANGED:
  161.         if (wParam != hWnd) {
  162.         HDC hDC = GetDC(hWnd);
  163.         HPALETTE holdPal = SelectPalette(hDC, _palette->GetPalette(), 0);
  164.         RealizePalette(hDC);
  165.         UpdateColors(hDC);
  166.         SelectPalette(hDC, holdPal, 0);
  167.         ReleaseDC(hDC, hWnd);
  168.         }
  169.         break;
  170.  
  171.     case WM_DESTROY:
  172.         if (hWnd == _world->hTimerWindow) {
  173.         PostQuitMessage(0);
  174.         }
  175.         break;
  176.  
  177.     default:
  178.         return (DefWindowProc(hWnd, message, wParam, lParam));
  179.         break;
  180.     }
  181.     return (0L);
  182. }
  183. }
  184.  
  185.  
  186. void RegisterChildClass () {
  187.      WNDCLASS wndclass;
  188.      do {
  189.       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  190.       wndclass.lpfnWndProc   = IVWindowProc;
  191.       wndclass.cbClsExtra    = 0;
  192.       wndclass.cbWndExtra    = 0;
  193.       wndclass.hInstance     = hInstance_;
  194.       wndclass.hIcon         = NULL;
  195.       wndclass.hCursor       = NULL;
  196.       wndclass.lpszClassName = "IVChild";
  197.       wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  198.       wndclass.lpszMenuName  = NULL;
  199.      }
  200.      while (!RegisterClass(&wndclass));
  201. }
  202.  
  203. void UnregisterChildClass () {
  204.     UnregisterClass("IVChild", hInstance_);
  205. }
  206.  
  207. HWND MakeTimerWindow () {
  208.     WNDCLASS wc;
  209.  
  210.     wc.style = NULL;
  211.     wc.lpfnWndProc = IVWindowProc;
  212.     wc.cbClsExtra = 0;
  213.     wc.cbWndExtra = 0;
  214.     wc.hInstance = hInstance_;
  215.     wc.lpszMenuName = NULL;
  216.     wc.lpszClassName = "DummyClass";
  217.     wc.hIcon = NULL;
  218.     wc.hCursor = NULL;
  219.     wc.hbrBackground = NULL;
  220.  
  221.     RegisterClass(&wc);
  222.  
  223.     return CreateWindow(
  224.            "DummyClass",
  225.            "Timer",
  226.            WS_POPUP,
  227.            0,
  228.            0,
  229.            10,
  230.            10,
  231.            NULL,
  232.            NULL,
  233.            hInstance_,
  234.            NULL
  235.       );
  236. }
  237.  
  238. WorldRep::WorldRep() {
  239.    _hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  240.    _max_palette_entry = 0;
  241.  
  242.    hTimerWindow = MakeTimerWindow();
  243.    SetTimer(hTimerWindow, TIMER_ENTERLEAVE, 200, NULL);
  244.  
  245.    XBorder = GetSystemMetrics(SM_CXFRAME);
  246.    YBorder = GetSystemMetrics(SM_CYFRAME);
  247.    YCaption = GetSystemMetrics(SM_CYCAPTION);
  248.  
  249.    RegisterChildClass();
  250. }
  251.  
  252. WorldRep::~WorldRep() {
  253.    DeleteDC(_hDC);
  254.    KillTimer(hTimerWindow, TIMER_ENTERLEAVE);
  255.    DestroyWindow(hTimerWindow);
  256.    UnregisterClass("DummyClass", hInstance_);
  257. }
  258.  
  259. HANDLE           WorldRep::hinstance()         { return _hinstance; };
  260. HANDLE           WorldRep::hprevinstance()     { return _hprevinstance; }
  261. char*            WorldRep::hostname()          { return _host; };
  262. HWND             WorldRep::root()              { return _root; };
  263. WORD             WorldRep::max_palette_entry() { return _max_palette_entry; };
  264. InteractorTable* WorldRep::itable()            { return _itable; };
  265. HDC              WorldRep::hdc()               { return _hDC; };
  266.